package com.han.fu.api.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.han.fu.api.business.entity.File;
import com.han.fu.api.business.entity.News;
import com.han.fu.api.business.entity.User;
import com.han.fu.api.business.enums.AuditStatusEnum;
import com.han.fu.api.business.enums.NewsTypeEnum;
import com.han.fu.api.business.mapper.NewsMapper;
import com.han.fu.api.business.model.ao.*;
import com.han.fu.api.business.model.vo.NewsBrowseVo;
import com.han.fu.api.business.model.vo.NewsInfoVo;
import com.han.fu.api.business.model.vo.NewsVo;
import com.han.fu.api.business.service.IFileService;
import com.han.fu.api.business.service.INewsService;
import com.han.fu.api.business.service.IUserService;
import com.han.fu.api.common.exception.BusinessException;
import com.han.fu.api.common.model.UserDetail;
import com.han.fu.api.common.utils.CurrentUserUtil;
import com.han.fu.common.constants.DeleteConstants;
import com.han.fu.common.utils.IdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 新闻接口实现类
 *
 * @author Star.zhu
 * @since 2020-03-26
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Autowired
    IFileService fileService;
    @Autowired
    IUserService userService;
    @Autowired
    CurrentUserUtil currentUserUtil;

    /**
     * 新闻分页查询
     *
     * @param ao 查询条件
     * @return Page<NewsVo> 分页数据
     */
    @Override
    public Page<NewsVo> queryNewsPage(NewsPageAo ao) {
        return baseMapper.queryNewsPage(ao);
    }

    /**
     * 新闻明细查询
     *
     * @param id 明细ID
     * @return
     */
    @Override
    public NewsInfoVo queryNewsDetail(String id) {
        NewsInfoVo newsInfoVo = new NewsInfoVo();
        News news = super.getById(id);
        if (null != news) {
            BeanUtils.copyProperties(news, newsInfoVo);
            newsInfoVo.setFiles(fileService.queryFilesByBusinessId(id));
        }
        return newsInfoVo;
    }

    /**
     * 新闻新增数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNews(NewsAddAo ao) {
        // 保存文件
        News entity = new News();
        BeanUtils.copyProperties(ao, entity);
        super.save(entity);
        // 保存文件
        if (CollectionUtils.isNotEmpty(ao.getFiles())) {
            for (String fileName : ao.getFiles()) {
                if (StringUtils.isNotBlank(fileName)) {
                    File file = new File();
                    file.setName(fileName);
                    file.setBusinessId(entity.getId());
                    file.setId(IdUtil.generate());
                    fileService.save(file);
                }
            }
        }
    }

    /**
     * 新闻修改数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNews(NewsUpdateAo ao) {
        UserDetail user = currentUserUtil.getUser();
        News news = super.getById(ao.getId());
        if (news.getCreateUser().equals(user.getId())) {
            news.setTitle(ao.getTitle());
            news.setContent(ao.getContent());
            news.setFirstImgName(ao.getFirstImgName());
            news.setStatus(AuditStatusEnum.NOT_APPROVED.getCode());
            super.updateById(news);
            // 删除文件
            fileService.updateByBusinessId(ao.getId());
            // 保存文件
            if (CollectionUtils.isNotEmpty(ao.getFiles())) {
                for (String fileName : ao.getFiles()) {
                    if (StringUtils.isNotBlank(fileName)) {
                        File file = new File();
                        file.setName(fileName);
                        file.setBusinessId(news.getId());
                        file.setId(IdUtil.generate());
                        fileService.save(file);
                    }
                }
            }
        } else {
            throw new BusinessException("无权限操作数据");
        }
    }

    /**
     * 新闻删除数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNews(NewsDeleteAo ao) {
        UserDetail user = currentUserUtil.getUser();
        News news = super.getById(ao.getId());
        if (Objects.nonNull(news) && news.getCreateUser().equals(user.getId())) {
            super.removeById(ao.getId());
            // 删除文件
            fileService.updateByBusinessId(ao.getId());
        } else {
            throw new BusinessException("无权限操作数据");
        }
    }

    /**
     * 新闻审批数据
     *
     * @param ao 数据
     */
    @Override
    public void auditNews(NewsAuditAo ao) {
        News entity = new News();
        entity.setId(ao.getId());
        entity.setStatus(ao.getStatus());
        super.updateById(entity);
    }

    /**
     * 新闻分页查询(用户端使用)
     *
     * @param ao 分页查询条件
     * @return Page<NewsBrowseVo> 分页数据
     */
    @Override
    public Page<NewsBrowseVo> queryBrowsePage(NewsBrowseAo ao) {
        UserDetail currentUser = currentUserUtil.getUser();
        if (NewsTypeEnum.FOLLOW.getCode() == ao.getType()) {
            ao.setUserId(currentUser.getId());
        } else if (NewsTypeEnum.SAME_CITY.getCode() == ao.getType()) {
            ao.setCity(currentUser.getCity());
        }
        Page<NewsBrowseVo> page = baseMapper.queryBrowsePage(ao);
        if (Objects.nonNull(page) && CollectionUtils.isNotEmpty(page.getRecords())) {
            for (NewsBrowseVo record : page.getRecords()) {
                User user = userService.getById(record.getCreateUser());
                record.setHeaderName(user.getHeaderName());
                record.setCreateUserName(user.getCreateUserName());
                record.setAuthentication(user.getAuthentication());
                record.setFiles(fileService.queryFilesByBusinessId(record.getId()));
            }
        }
        return page;
    }

}